package com.budwk.app.popo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.popo.PopoUtils;
import com.budwk.app.popo.bo.PopoUserBO;
import com.budwk.app.popo.service.PopoInterfaceService;
import com.budwk.app.sqfw.services.SqfwPoUserService;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.util.HttpUtils;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;


@IocBean
public class PopoInterfaceServiceImpl implements PopoInterfaceService {

    @Inject
    private SqfwPoUserService sqfwPoUserService;

    private final String popoServerUrl = "http://140.210.211.155:9095";

    @Override
    public String addPopoUser(String userName, String nickName, String avatarUrl, String password, String sign) {

        // wzy 2023-5-29 因为现在都是根据电话号码进行添加的，如果这个电话号码已经存在就直接返回
        PopoUserBO popoUser = getPopoUser(null, userName, "123456", "1");
        if (popoUser != null) return popoUser.getUid();

        String baseUrl = popoServerUrl + "/api/user/add";

        if (StringUtils.isBlank(avatarUrl)) avatarUrl = "/static/avatar.jpg";

        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("username", userName);
        paramMap.put("nickname", nickName);
        paramMap.put("avatar", avatarUrl);
        paramMap.put("password", password);
        if (StringUtils.isNotBlank(sign)) paramMap.put("sign", sign);

        String responseStr = HttpUtils.postRequest(PopoUtils.getPoPoTokenUrl(baseUrl), paramMap);
        JSONObject responseJsonObj = JSONObject.parseObject(responseStr);
        Integer code = responseJsonObj.getInteger("code");
        if (code != null && code.equals(0)) {
            JSONObject dataJsonObj = responseJsonObj.getJSONObject("data");
            String newPopoUserUid = dataJsonObj.getString("uid");
            if (StringUtils.isNotBlank(newPopoUserUid)) return newPopoUserUid;
        }

        return null;
    }

    @Override
    public Integer updatePopoUser(String loginUid, String nickname, String avatar, String sign, String accountState) {
        String baseUrl = popoServerUrl + "/api/user/update";

        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("login_uid", loginUid);
        paramMap.put("nickname", nickname);
        paramMap.put("avatar", avatar);
        paramMap.put("sign", sign);
        paramMap.put("account_state", accountState);

        String responseStr = HttpUtils.postRequest(PopoUtils.getPoPoTokenUrl(baseUrl), paramMap);
        JSONObject responseJsonObj = JSONObject.parseObject(responseStr);
        Integer code = responseJsonObj.getInteger("code");
        if (code != null && code.equals(0)) {
            return 1;
        } else return 0;
    }

    @Override
    public String addPopoUser(Sys_user sysUser) {
        return addPopoUser(sysUser.getLoginname(), sysUser.getUsername(), null, "123456", "");
    }

    @Override
    public PopoUserBO getPopoUser(String uid, String userName, String password, String loginUid) {
        String baseUrl = popoServerUrl + "/api/user/get";

        Map<String, Object> paramMap = Maps.newHashMap();
        if (StringUtils.isNotBlank(uid)) paramMap.put("uid", uid);
        if (StringUtils.isNotBlank(userName)) paramMap.put("username", userName);
        if (StringUtils.isNotBlank(password)) paramMap.put("password", password);
        paramMap.put("login_uid", loginUid);

        String responseStr = HttpUtils.getRequest(baseUrl, paramMap);
        JSONObject responseJsonObj = JSONObject.parseObject(responseStr);
        Integer responseCode = responseJsonObj.getInteger("code");
        if (responseCode != null && responseCode.equals(0)) {

            JSONObject dataJsonObj = responseJsonObj.getJSONObject("data");
            PopoUserBO bo = new PopoUserBO();
            bo.setUid(dataJsonObj.getString("uid"));
            bo.setUsername(dataJsonObj.getString("username"));
            bo.setNickname(dataJsonObj.getString("nickname"));
            bo.setSign(dataJsonObj.getString("sign"));
            bo.setAvatar(dataJsonObj.getString("avatar"));
            bo.setState(dataJsonObj.getString("state"));
            bo.setAccountState(dataJsonObj.getString("account_state"));
            bo.setFriendFlag(dataJsonObj.getBoolean("is_friend"));
            bo.setRemark(dataJsonObj.getString("remark"));
            return bo;
        } else return null;
    }

    @Override
    public String sendMessage(String loginUid, String to, String type, String content) {
        String baseUrl = popoServerUrl + "/api/message/send";

        // 调用接口发送短信
        Map<String, Object> messageParamMap = Maps.newHashMap();
        messageParamMap.put("login_uid", loginUid);
        messageParamMap.put("to", to);
        messageParamMap.put("type", type);
        messageParamMap.put("content", content);

        String responseStr = HttpUtils.postRequest(PopoUtils.getPoPoTokenUrl(baseUrl), messageParamMap);
        JSONObject responseJsonObj = JSONObject.parseObject(responseStr);
        Integer responseCode = responseJsonObj.getInteger("code");
        if (responseCode != null && responseCode.equals(0)) {
            JSONObject dataJsonObj = responseJsonObj.getJSONObject("data");
            return dataJsonObj.getString("mid");
        }
        return null;
    }

    @Override
    public Integer addFriend(String loginUid, String friendUid) {
        String baseUrl = popoServerUrl + "/api/friend/add";

        Map<String, Object> messageParamMap = Maps.newHashMap();
        messageParamMap.put("login_uid", loginUid);
        messageParamMap.put("friend_uid", friendUid);

        String responseStr = HttpUtils.postRequest(PopoUtils.getPoPoTokenUrl(baseUrl), messageParamMap);
        JSONObject responseJsonObj = JSONObject.parseObject(responseStr);
        Integer responseCode = responseJsonObj.getInteger("code");
        if (responseCode != null && responseCode.equals(0)) return 1;
        else return 0;
    }

    @Override
    public Integer uploadAvatar(InputStream inputStream, String fileName, String popoUid) {

        // 首先上传文件
        String baseUrl = popoServerUrl + "/api/upload/avatar";

        Map<String, Object> messageParamMap = Maps.newHashMap();
        messageParamMap.put("login_uid", popoUid);

        String responseStr = HttpUtils.uploadFileWithParams(PopoUtils.getPoPoTokenUrl(baseUrl), fileName, inputStream, messageParamMap);
        JSONObject responseJsonObj = JSONObject.parseObject(responseStr);
        Integer responseCode = responseJsonObj.getInteger("code");
        if (responseCode == null || !responseCode.equals(0)) return 0;

        JSONObject dataJsonObj = responseJsonObj.getJSONObject("data");
        String popoAvatarUrl = dataJsonObj.getString("src");

        PopoUserBO popoUser = getPopoUser(popoUid, null, null, popoUid);
        if (popoUser == null) return 0;

        popoUser.setAvatar(popoAvatarUrl);
        // 然后更改这个用户的信息
        Integer updateCount = updatePopoUser(popoUid, popoUser.getNickname(), popoUser.getAvatar(), popoUser.getSign(), popoUser.getAccountState());
        if (updateCount != null && updateCount.equals(1)) return 1;

        return 0;
    }

    @Override
    public Integer deletePopoUser(List<String> userIdList) {
        int deleteCount = 0;
        for (String popoUserId : userIdList) {
            String baseUrl = popoServerUrl + "/api/user/delete";

            Map<String, Object> messageParamMap = Maps.newHashMap();
            messageParamMap.put("uid ", popoUserId);

            String responseStr = HttpUtils.postRequest(PopoUtils.getPoPoTokenUrl(baseUrl), messageParamMap);
            JSONObject responseJsonObj = JSONObject.parseObject(responseStr);
            Integer responseCode = responseJsonObj.getInteger("code");
            if (responseCode != null && responseCode.equals(0)) deleteCount++;
        }
        return deleteCount;
    }

    @Override
    public Integer revokeMessage(String loginUid, String popoUid, String popoMid) {
        String baseUrl = popoServerUrl + "/api/message/revoke";

        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("login_uid", loginUid);
        paramMap.put("id", popoUid);
        paramMap.put("type", "friend");
        paramMap.put("mid", popoMid);

        int revokeCount = 0;

        String responseStr = HttpUtils.postRequest(PopoUtils.getPoPoTokenUrl(baseUrl), paramMap);
        JSONObject responseJsonObj = JSONObject.parseObject(responseStr);
        Integer responseCode = responseJsonObj.getInteger("code");
        if (responseCode != null && responseCode.equals(0)) revokeCount++;

        return revokeCount;
    }
}
